var http = require('../../../utils/https.js')
Page({
  data: {
    // 当前地址
    address:"东石马小区南门",
    userInfo:{},

    // 我的收货地址列表
    deliveryAddresses: [
      {
        id:1,
        address: '光谷软件园2号楼西门1111',
        room: '2号楼101室',
        name: '西瓜',
        gender: '女士',
        phone: '18888883945',
        selected: true,
        check:false
      }
    ],
    // 附近地址列表
    nearbyAddresses: [
      {
        name: '沙县小吃',
        address: '武汉市光谷软件园沙县小吃33栋105号'
      },
      {
        name: '锦心大药房',
        address: '武汉市光谷软件园沙县小吃33栋105号'
      }
    ]
  },
  
  // 更改我的收获地址
  onChangesAddress(e){
//    console.log(e.detail.value);
    var index = e.detail.value
    console.log(index);
    console.log(this.data.deliveryAddresses);
    var addressList = this.data.deliveryAddresses
    var t = 0
    for(var i = 0;i<addressList.length;i++){
        if(addressList[i].id==index){
            addressList[i].isDefault=1
            t = i
        }else{
            addressList[i].isDefault=0
        }
    }
    var nowAddress = addressList[t].city+" "+" "+addressList[t].district+" "+addressList[t].details
    this.setData({
        deliveryAddresses:addressList,
        address:nowAddress
    })
    http.PUT('/user/address', addressList[t], function(res) {
        console.log('成功:', res);
      }, function(err) {
        console.error('失败:', err);
      });


    // var address = this.data.deliveryAddresses[index-1].address
    // this.setData({
    //   address:address
    // }) 
  },

  // 获取用户地址
  getAddress(userId){
    var that = this
    http.GET('/user/address/listByUserId', { 
        userId:userId
     }, function(res) {
        if(res.code===200){
            console.log(res.data);
            var a = res.data
            that.setData({
                deliveryAddresses:a,
            })
            var index = 0
            // 无默认时 第一个地址为默认
            for(var i = 0;i<a.length;i++){
                if(a[i].isDefault==1){
                    index = i
                    break
                }
            }
            var nowAddress = a[index].city+" "+a[index].district+" "+a[index].details
            that.setData({
                address:nowAddress
            })


        }else{
            wx.showToast({
              title: '登录过期',
              icon:'none',
              duration:1000
            })
            setTimeout(()=>{
                wx.redirectTo({
                  url: '/index/pages/mzhLogin/mzhLogin',
                })
            },1500)
        }
      }, function(err) {
        console.error('失败:', err);
      });
  },


  onLoad: function (options) {
      var userInfo = wx.getStorageSync('userInfo')
      if(userInfo!=null&&userInfo!=undefined){
        this.setData({
            userInfo:userInfo
        })
        this.getAddress(userInfo.userId)
        
        
        
      }

  },

  // 返回上一页
  onBackTap: function() {
    wx.navigateBack();
  },

  // 搜索地址
  onSearchInput: function(e) {
    const value = e.detail.value;
    // 这里可以添加搜索逻辑
    console.log('搜索:', value);
  },




  // 添加新地址
  onAddAddress: function() {
    wx.navigateTo({
      url:"/my/pages/mzhAddressAdd/mzhAddressAdd?userId="+this.data.userInfo.userId
    });
  }
});